# CMake 最低版本号要求
cmake_minimum_required(VERSION 3.30 FATAL_ERROR)
set(project_name "hello")
set(oformat "elf64") # elf64, elf32

set(CMAKE_NASM_LINK_EXECUTABLE
    "ld <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
set(CMAKE_ASM_NASM_LINK_EXECUTABLE
    "ld <CMAKE_ASM_NASM_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>"
)

set(CMAKE_ASM_NASM_COMPILE_OBJECT
    "<CMAKE_ASM_NASM_COMPILER> <INCLUDES> <FLAGS> -o <OBJECT> <SOURCE>")

set(CAN_USE_ASSEMBLER TRUE)

if(APPLE)
  set(CMAKE_ASM_NASM_OBJECT_FORMAT macho64)
  set(CMAKE_ASM_NASM_FLAGS "-DMACOS")
  set(CMAKE_EXE_LINKER_FLAGS
      "${CMAKE_EXE_LINKER_FLAGS} -macosx_version_min 10.13 -no_pie -L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib"
  )
endif(APPLE)

if(UNIX AND NOT APPLE)
  set(CMAKE_ASM_NASM_OBJECT_FORMAT ${oformat})
endif(UNIX AND NOT APPLE)

# 指定 C++ 标准
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# FLAGS
set(CMAKE_ASM_NASM_FLAGS "-g") # 让NASM生成调试信息
# set(CMAKE_CXX_FLAGS "-no-pie") # 让GCC不使用PIE模式

# Version
set(Demo_VERSION_MAJOR 0)
set(Demo_VERSION_MINOR 1)

# 设置NASM的文件扩展名为asm,nasm, s, S
set(CMAKE_ASM_NASM_SOURCE_FILE_EXTENSIONS
    ${CMAKE_ASM_NASM_SOURCE_FILE_EXTENSIONS} s S)

# 源码
set(SOURCE_FILES src/asm/hello.asm)
set_source_files_properties(${SOURCE_FILES} PROPERTIES LANGUAGE ASM_NASM)

# 项目信息
project(
  ${project_name}
  VERSION 0.1
  LANGUAGES ASM_NASM)

add_library(Along32 STATIC
    src/Along32/Along32.asm
)


add_compile_options(
  "$<$<COMPILE_LANGUAGE:ASM_NASM>:-f $<IF:$<BOOL:$<TARGET_PROPERTY:NASM_OBJ_FORMAT>>, \
  $<TARGET_PROPERTY:NASM_OBJ_FORMAT>, ${CMAKE_ASM_NASM_OBJECT_FORMAT}>>")

# if(CMAKE_ASM_NASM_COMPILER_LOADED) set(CAN_USE_ASSEMBLER TRUE)
# file(GLOB_RECURSE ENGINE_ASM_FILES "src/engine/*.asm") set(ENGINE_SOURCES
# ${ENGINE_SOURCES} ${ENGINE_ASM_FILES}) endif(CMAKE_ASM_NASM_COMPILER_LOADED)
# SET(SRC_LIST hello.cpp) configure_file(config.h.in config.h)

enable_language(ASM_NASM) # 让CMacke启用NASM的汇编

# 指定生成目标
add_executable(${project_name} ${SOURCE_FILES})

# 目标设置
set_target_properties(${project_name} PROPERTIES LINKER_LANGUAGE NASM)
set_target_properties(${project_name} PROPERTIES NASM_OBJ_FORMAT ${oformat})

if(APPLE)
  target_link_libraries(${project_name} System Along32)
endif(APPLE)

if(UNIX AND NOT APPLE)
  target_link_libraries(${project_name} Along32)
endif(UNIX AND NOT APPLE)

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  target_compile_options(test PRIVATE -g -Fdwarf)
endif()
